app.controller("transferController",['$scope','$http','myPage', function($scope, $http, myPage){
    // 超级管理员权限
    $http({
        method: 'GET',
        url: '/OA/staff/roles',
        headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
    }).success(function (response) {
        $scope.roles=[];
        $scope.roles=response.data;
        console.log($scope.roles)
        function permissions(){
            var rs=false;
            for (var i = 0; i < $scope.roles.length; i++) {
                if ($scope.roles[i] == 7) {
                    rs = true;
                }else{
                    rs= false;
                }

            }
            return rs
        }
        $scope.admin=permissions();
        console.log($scope.admin)
    })
        $scope.myPage={
            currentPage:1,//访问第几页数据，从1开始
            totalItems:0,//数据库中总共有多少条数据
            itemsPerPage: 6,//默认每页展示多少条数据，可更改
            pagesLength: 15,
            perPageOptions: [10, 20, 30, 40, 50,60]//可选择的每页展示多少条数据
        };
        //监测当页码。总数据，每页展示数据个数变化时，重新加载数据
        $scope.$watch(function ()
        {return $scope.myPage.itemsPerPage+' '+$scope.myPage.currentPage+' '+$scope.myPage.totalItems;},getList) ;
        function getList(){
            //获取列表需要时，将页码重置为1
            $scope.myPage.currentPage=myPage.pageNub;
            //传给服务器时，页码从0开始
            // 定义查询按钮按照校区查询
            var listJson={};
            function changeJson(){
                var   campus_id=$("#schoolcampus").find("option:selected").val();
                return {pageNumber:myPage.pageNub,campus_id:campus_id}
            }
            listJson=changeJson();
            $scope.find=function () {
                listJson= changeJson();
                Json();
            };
            Json();
            // 获取列表接口
            function  Json() {
                $http({
                    method: 'POST',
                    url: '/OA/one2one/list',
                    data: $.param(listJson),
                    headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
                }).success(function (response) {
                    console.log(response.data)
                    $scope.datas=response.data.list;
                    $scope.pageSize=response.data.pageSize;
                    $scope.pageNumber=response.data.pageNumber;
                    $scope.myPage.totalItems=response.data.totalRow;//当获取总数据后，修改默认值;
                    // 定义一个空对象 , 用于更新和保存数据时临时存储
                    $scope.prod = {};
                    $scope.arrs=$scope.datas;
                    // 报课
                    $scope.enrol=function ($index) {
                        $("#dailyCourse").modal("show");
                        $scope.pact_id=$scope.arrs[$index].id;
                        $scope.save=function () {
                            var pact_id=$scope.pact_id;
                            var sn=$scope.sn;
                            var teach_5a_price=$scope.teach_5a_price;
                            var teach_5a_hour=$scope.teach_5a_hour;
                            var teach_5a_price_total=$("#teach_5a_price_total").val();
                                teach_5a_price_total=Number(teach_5a_price_total)
                            var teach_8a_price=$scope.teach_8a_price;
                            var teach_8a_hour=$scope.teach_8a_hour;
                            var teach_8a_price_total=$("#teach_8a_price_total").val();
                                teach_8a_price_total=Number(teach_8a_price_total);
                            var teach_director_price=$scope.teach_director_price;
                            var teach_director_hour=$scope.teach_director_hour;
                            var teach_director_price_total=$("#teach_director_price_total").val();
                                teach_director_price_total=Number(teach_director_price_total)
                            var method_id=$("#methostyle").find("option:selected").val();
                            var method_name=$("#methostyle").find("option:selected").text();
                            var tuition_reality=$scope.tuition_reality;
                            var tuition_lack=$("#tuition_lack").val()
                                tuition_lack=Number(tuition_lack)
                            var tuition_total=$("#tuition_total").val();
                                tuition_total=Number(tuition_total);
                            var principal=$scope.principal;
                            var parents=$scope.parents;
                            var record_sn=$scope.record_sn;
                            $scope.Json={pact_id:pact_id,sn:sn,teach_5a_price:teach_5a_price,teach_5a_hour:teach_5a_hour,teach_5a_price_total:teach_5a_price_total,teach_8a_price:teach_8a_price,teach_8a_hour:teach_8a_hour,teach_8a_price_total:teach_8a_price_total,teach_director_price:teach_director_price,teach_director_hour:teach_director_hour,teach_director_price_total:teach_director_price_total,method_id:method_id,method_name:method_name,tuition_reality:tuition_reality,tuition_lack:tuition_lack,tuition_total:tuition_total,principal:principal,parents:parents,record_sn:record_sn}
                            console.log($scope.Json)
                            $http({
                                method:"POST",
                                url:"/OA/enrolone2one/create",
                                data:$.param($scope.Json),
                                headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
                            }).success(function (response) {
                                if(response.ok==true){
                                    $("#dailyCourse").modal("hide");
                                    setTimeout(function () {
                                        window.location.reload()
                                    },600)
                                }
                            })
                        }

                    }
                    // 计算和
                    $("#tuition_total").focus(function () {
                        var teach_director_price_total=$("#teach_director_price_total").val();
                        var teach_8a_price_total=$("#teach_8a_price_total").val();
                        var teach_5a_price_total=$("#teach_5a_price_total").val();
                        teach_5a_price_total=Number(teach_5a_price_total);
                        teach_8a_price_total=Number(teach_8a_price_total);
                        teach_director_price_total=Number(teach_director_price_total);
                        $("#tuition_total").val(teach_5a_price_total+teach_8a_price_total+teach_director_price_total)
                        $scope.tuition_total=$("#tuition_total").val();
                    })
                    // 补交
                    $scope.supplementary=function ($index) {
                        $("#pay").modal("show");
                        $scope.prod.tuition_lack=$scope.arrs[$index].tuition_lack;
                        $scope.prod.tuition_reality=$scope.arrs[$index].tuition_reality;
                        $scope.prod.tuition_total=$scope.arrs[$index].tuition_total;
                        $scope.prod.pact_id=$scope.arrs[$index].id;
                        $scope.increase=function () {
                            var method_id=$("#methodstyle").find("option:selected").val();
                            if(method_id==""){
                                alert("请选择缴费方式！");
                                return
                            }
                            var method_name=$("#methodstyle").find("option:selected").text();
                            var amount=Number($scope.nowsupplementary);
                            var pact_id=$scope.prod.pact_id;
                            var record_sn=$scope.prodtuition_record_sn;
                            $scope.JSON={method_id:method_id,method_name:method_name,amount:amount,pact_id:pact_id,record_sn:record_sn};
                            $http({
                                method:"POST",
                                url:"/OA/mend/create",
                                data:$.param($scope.JSON),
                                headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
                            }).success(function (response) {
                                if(response.ok==true){
                                    $("#pay").modal("hide");
                                    setTimeout(function () {
                                        window.location.reload()
                                    },600)
                                }
                            })
                        }
                    }
                    // 一对一合同退课
                    $scope.drop={};
                    $scope.drop=function ($index) {
                        $("#withdraw").modal('show');
                        $scope.drop.id=$scope.arrs[$index].id;
                        $scope.drop.teach_5a_hour_surplus=$scope.arrs[$index].teach_5a_hour_surplus;
                        $scope.drop.teach_8a_hour_surplus=$scope.arrs[$index].teach_8a_hour_surplus;
                        $scope.drop.teach_director_hour_surplus=$scope.arrs[$index].teach_director_hour_surplus;
                        $scope.dropsave=function () {
                        var teach_5a_price=$("#drop5aprice").val();
                        var teach_8a_price=$("#drop8aprice").val();
                        var teach_director_price=$("#dropdirectorprice").val();
                        var drop_5a_hour=$("#drop_teach_5a_hour_surplus").val();
                        var drop_8a_hour=$("#drop_teach_8a_hour_surplus").val();
                        var drop_director_hour=$("#drop_teach_director_hour_surplus").val();
                        var principal=$scope.dropheadmaster;
                        var parents=$scope.dropparents;
                        var drop_total=$('#droptuition').val();
                        var record_sn=$("#record_sn").val();
                        var pact_id=$scope.drop.id;
                        $scope.json={pact_id:pact_id,teach_5a_price:teach_5a_price,teach_8a_price:teach_8a_price,teach_director_price:teach_director_price,drop_5a_hour:drop_5a_hour,drop_8a_hour:drop_8a_hour,drop_director_hour:drop_director_hour,principal:principal,parents:parents,drop_total:drop_total,record_sn:record_sn};
                            $http({
                                method:"POST",
                                url:"/OA/dropone2one/create",
                                data:$.param($scope.json),
                                headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
                            }).success(function (response) {
                                if(response.ok==true){
                                    alert("退课成功!")
                                    $("#withdraw").modal('hide')
                                    setTimeout(function () {
                                        window.location.reload()
                                    },500)
                                }else if(response.ok==false){
                                    alert("退课失败！")
                                }
                            })
                        }
                    }
                    // 计算退课总学费
                    $scope.focusdrop=function () {
                        $("#droptuition").val(Number($("#drop_teach_5a_hour_surplus").val()*300)+$("#drop_teach_8a_hour_surplus").val()*400+$("#drop_teach_director_hour_surplus").val()*600)
                    }
                })
            }
        }

        $scope.change = function(selectedLimit){
            $scope.page.limit = selectedLimit.limit;
        }
        $scope.pageChanged = function(){
            $scope.page.limit = $scope.selectedLimit.limit;
        }
        $scope.setPage = function (go) {
            $scope.length = Math.ceil($scope.total/$scope.selectedLimit.limit);
            console.log($scope.length);
            if(go > $scope.length){
                $scope.page.pageNo =  $scope.length;
                console.log($scope.length);
            }else{
                $scope.page.pageNo=go;
            }
        };
    // 检测是否存在
    $scope.detect=function () {
        var studentcard=$scope.student_sn;
        $scope.JSON={property:"sn",value:studentcard};
        console.log($scope.JSON)
        $http({
            method:"POST",
            url:"/OA/student/exist",
            data:$.param($scope.JSON),
            headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
        }).success(function (response) {
            console.log(response)
            if(response.ok==true){
                $("#prompting").html("应用通过，卡号存在");
            }else if(response.ok==false){
                $("#prompting").html("应用未通过，卡号不存在");
            }
        })
    };
    // 咨询教务切换
    $("#presonposition").change(function () {
        var role_id=$("#presonposition").val();
        $.ajax({
            type:"POST",
            url:"/OA/staff/find",
            data:{role_id:role_id},
            dataType:"json",
            success:function (response) {
                $("#presonname").html("");
                for (var i = 0; i < response.data.length; i++) {
                    $("#presonname").append('<option value='+response.data[i].id+'>'+response.data[i].username+'</option>')
                };
            }
        })
    })
    // 创建合同
    $scope.save=function(){
        var role_id=$("#presonposition").find("option:selected").val();
        var role_name=$("#presonposition").find("option:selected").text();
        var name=$scope.name;
        var student_sn=$scope.student_sn;
        function staff_type() {
            if($("#presonposition").find("option:selected").val()=="1"){
                return 1
            }else if($("#presonposition").find("option:selected").val()=="3"){
                return 2
            }
        }
        var staff_type=staff_type();
        function staff_id() {
            if($("#presonname").find("option:selected").val()==""){
                alert("请选择一位咨询或者教务");
                return false
            }else{
                return $("#presonname").find("option:selected").val();
            }
        }
        var staff_id=staff_id();
        if(staff_id==false)
            return;
        $scope.addstudent={name:name,student_sn:student_sn,staff_type:staff_type,staff_id:staff_id,role_id:role_id,role_name:role_name}
        $http({
            method: 'POST',
            url: '/OA/one2one/create',
            data: $.param($scope.addstudent),
            headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
        }).success(function (response){
            if (response.ok== true) {
                $("#createContract").modal('hide');
                setTimeout(function () {
                    window.location.reload()
                },500)
            };
        })
    }
    }]);


